﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-08>
//创建作者：<朱大鹏，zhudapeng@shanghai3h.com>
//功能说明：开账
//********************************************************************************//
using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Common;

namespace Web.Pages.YongShui._4.ChaoBiaoKZ
{
    public partial class KaiZhang : BasePage
    {
        #region 属性
        #endregion

        #region 变量
        /// <summary>
        /// 数据库
        /// </summary>
        SpHelper _spHelper = new SpHelper();
        #endregion 

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();

            }
        }
        /// <summary>
        /// 查错按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSelectError_Click(object sender, EventArgs e)
        {
            LabNum.Visible = false;
            SearchError();
        }
        /// <summary>
        /// /汇总按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnKZ_Click(object sender, EventArgs e)
        {
            LabNum.Visible = false;
            SaveKaiZhang();
        }
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            if (e.Parameter == "checkerror")
            {

                int code = -1;
                string msg = "";

                string newids = "";
                if (HiddenList.Contains("glkcheck"))
                {
                    string glk = HiddenList.Get("glkcheck").ToString().Split(',')[0];
                    string[] ids = HiddenList.Get("glkcheck").ToString().Split(',');

                    if (glk.Trim().Length > 0)
                    {
                        foreach (string s in ids)
                        {
                            if (s.Trim() != glk.Trim())
                            {
                                newids += s + ",";
                            }
                        }
                        msg = CheckError(glk);
                        code = 1;
                    }
                    else
                    {
                        code = 2;
                    }
                }
                e.Result = e.Parameter + "|" + code + "|" + msg + "|" + newids;
            }
            if (e.Parameter == "sumdata")
            {
                int code = -1;
                string msg = "";

                string newids = "";
                if (HiddenList.Contains("glk"))
                {
                    string glk = HiddenList.Get("glk").ToString().Split(',')[0];
                    string[] ids = HiddenList.Get("glk").ToString().Split(',');

                    if (glk.Trim().Length > 0)
                    {
                        foreach (string s in ids)
                        {
                            if (s.Trim() != glk.Trim())
                            {
                                newids += s + ",";
                            }
                        }
                        msg = ToKaiZhang(glk);
                        code = 1;
                    }
                    else
                    {
                        code = 2;
                    }
                }
                e.Result = e.Parameter + "|" + code + "|" + msg + "|" + newids;
            }
        }

        #endregion

        #region 方法
        /// <summary>
        /// 页面首次加载绑定控件
        /// </summary>
        private void InitControl()
        {
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            IYIM.Text = string.Format("{0}年{1}月", yd.Substring(0, 4), yd.Substring(4, 2));

            //获取当前用户站点信息
            string zhandianBH = BusinessTool.ZHANDIANBH();
            YS_JX_GUANLIKBLL glkbll = new YS_JX_GUANLIKBLL();

            DataTable glkdt = glkbll.SelectYS_JX_GUANLIKsDynamic(" I_JLZT=0 and (S_ZHANDIAN='" + zhandianBH + "' or '00'='" + zhandianBH + "')", " S_GUANLIKH");

            DrpQK.DataSource = glkdt;
            DrpQK.TextField = "S_GUANLIKMC";
            DrpQK.ValueField = "S_GUANLIKH";
            DrpQK.DataBind();

            if (zhandianBH == "00")
            {
                DrpQK.Items.Insert(0, new DevExpress.Web.ListEditItem("所有区块", "0"));
            }
            if (glkdt != null && glkdt.Rows.Count > 0)
            {
                string ids = "";
                foreach (DataRow dr in glkdt.Rows)
                {
                    ids += dr["S_GUANLIKH"] + ",";
                }
                glklist.Value = ids;
            }

            DrpQK.SelectedIndex = 0;

          
            txtZhuanHuanJG.Visible = true;
            
        }
        /// <summary>
        /// 查错
        /// </summary>
        private void SearchError()
        {
           
            txtZhuanHuanJG.Visible = true;
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());

            YS_YG_YONGHUXXBLL yhxxbll = new YS_YG_YONGHUXXBLL();


            YS_CB_HUIZONGCWBLL hzxxbll = new YS_CB_HUIZONGCWBLL();
            string SGUANLIKH = "";

            if (DrpQK.Value == null)
            {
                txtZhuanHuanJG.Text = "管理块不存在";
                return;
            }
            SGUANLIKH = DrpQK.Value.ToString();

            DataTable dtI_GUANBIAO = WordsHelper.GetXiaoLeiListByWordsID(10);
            int StartMonth = 0;
            int EndMnth = 0;
            if (dtI_GUANBIAO != null && dtI_GUANBIAO.Rows.Count > 0)
            {
                foreach (DataRow dr in dtI_GUANBIAO.Rows)
                {
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表起始月")
                    {
                        StartMonth = Convert.ToInt32(dr["WordsValue"]);
                    }
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表中止月")
                    {
                        EndMnth = Convert.ToInt32(dr["WordsValue"]);
                    }
                }
            }
            int syear = 0;
            int smonth = 0;

            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }
            string allmsg = "";
            string Msg = "";
            int? ErrCount = 0;
            string ErrMsg = "";
            int userid=LoginInfo.CurrentUser.UserID;
            foreach (DevExpress.Web.ListEditItem li in DrpQK.Items)
            {
                if (li.Value.ToString() != "0" && (DrpQK.Value.ToString() == "0" || (DrpQK.Value == li.Value && DrpQK.Value.ToString() != "0")))
                {
                    DataSet ds = _spHelper.P_YS_CB_KAIZHANGCC(li.Value.ToString(), syear, smonth, StartMonth, EndMnth, 0, userid.ToString(), out ErrCount, out ErrMsg);
                    DataTable dt = ds.Tables[0];
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        allmsg = allmsg+li.Text + "：" + dt.Rows[0][0].ToString() + "\r\n";
                    }
                    else
                    {

                    }
                }
            }

            txtZhuanHuanJG.Text = allmsg;
        }
        /// <summary>
        /// 查错
        /// </summary>
        private string CheckError(string glk)
        {


            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());

            YS_CB_HUIZONGCWBLL hzxxbll = new YS_CB_HUIZONGCWBLL();


            DataTable dtI_GUANBIAO = WordsHelper.GetXiaoLeiListByWordsID(10);
            int StartMonth = 0;
            int EndMnth = 0;
            if (dtI_GUANBIAO != null && dtI_GUANBIAO.Rows.Count > 0)
            {
                foreach (DataRow dr in dtI_GUANBIAO.Rows)
                {
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表起始月")
                    {
                        StartMonth = Convert.ToInt32(dr["WordsValue"]);
                    }
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表中止月")
                    {
                        EndMnth = Convert.ToInt32(dr["WordsValue"]);
                    }
                }
            }
            int syear = 0;
            int smonth = 0;

            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }
            string allmsg = "";
            string Msg = "";
            int? ErrCount = 0;
            string ErrMsg = "";
            int userid = LoginInfo.CurrentUser.UserID;

            DataSet ds = _spHelper.P_YS_CB_KAIZHANGCC(glk, syear, smonth, StartMonth, EndMnth, 0, userid.ToString(), out ErrCount, out ErrMsg);
            DataTable dt = ds.Tables[0];
            DrpQK.Value = glk;
            if (dt != null && dt.Rows.Count > 0)
            {
                allmsg = DrpQK.Text + "：" + dt.Rows[0][0].ToString() + "\r\n";
            }

            return allmsg;
        }
        /// <summary>
        /// 开账操作
        /// </summary>
        private void SaveKaiZhang()
        {
            
            txtZhuanHuanJG.Visible = true;
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());

            YS_YG_YONGHUXXBLL yhxxbll = new YS_YG_YONGHUXXBLL();


            YS_CB_HUIZONGCWBLL hzxxbll = new YS_CB_HUIZONGCWBLL();
            string SGUANLIKH = "";


            SGUANLIKH = DrpQK.Value.ToString();

            DataTable dtI_GUANBIAO = WordsHelper.GetXiaoLeiListByWordsID(10);
            int StartMonth = 0;
            int EndMnth = 0;
            if (dtI_GUANBIAO != null && dtI_GUANBIAO.Rows.Count > 0)
            {
                foreach (DataRow dr in dtI_GUANBIAO.Rows)
                {
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表起始月")
                    {
                        StartMonth = Convert.ToInt32(dr["WordsValue"]);
                    }
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表中止月")
                    {
                        EndMnth = Convert.ToInt32(dr["WordsValue"]);
                    }
                }
            }
            int syear = 0;
            int smonth = 0;

            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }
            string Msg = "";
            string ErrMsg = "";
            string allmsg = "";
            foreach (DevExpress.Web.ListEditItem li in DrpQK.Items)
            {
                if (li.Value.ToString() != "0" && (DrpQK.Value.ToString() == "0" || (DrpQK.Value == li.Value && DrpQK.Value.ToString()!="0")))
                {
                    DataSet ds = _spHelper.P_YS_CB_KAIZHANG(li.Value.ToString(), syear, smonth, StartMonth, EndMnth, 0, LoginInfo.CurrentUser.Account);
                    DataTable dt = ds.Tables[0];
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        
                        if (string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
                        {
                            allmsg = allmsg+li.Text + "：开账完成\r\n";
                          
                        }
                        else if (dt.Rows[0][0].ToString() == "没有错误，可以开帐；")
                        {
                            allmsg = allmsg + li.Text + "：无数据需要开账\r\n";
                           
                        }
                        else
                        {
                            allmsg = allmsg + li.Text + "：" + dt.Rows[0][0].ToString() + "\r\n";
                        }
                    }
                }
            }
            txtZhuanHuanJG.Text = allmsg;
            
        }
        private string ToKaiZhang(string glk)
        {


            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());


            YS_CB_HUIZONGCWBLL hzxxbll = new YS_CB_HUIZONGCWBLL();


            DataTable dtI_GUANBIAO = WordsHelper.GetXiaoLeiListByWordsID(10);
            int StartMonth = 0;
            int EndMnth = 0;
            if (dtI_GUANBIAO != null && dtI_GUANBIAO.Rows.Count > 0)
            {
                foreach (DataRow dr in dtI_GUANBIAO.Rows)
                {
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表起始月")
                    {
                        StartMonth = Convert.ToInt32(dr["WordsValue"]);
                    }
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表中止月")
                    {
                        EndMnth = Convert.ToInt32(dr["WordsValue"]);
                    }
                }
            }
            int syear = 0;
            int smonth = 0;

            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }
            string Msg = "";
            string ErrMsg = "";
            string allmsg = "";

            DataSet ds = _spHelper.P_YS_CB_KAIZHANG(glk, syear, smonth, StartMonth, EndMnth, 0, LoginInfo.CurrentUser.UserID.ToString());
            DataTable dt = ds.Tables[0];
            DrpQK.Value = glk;
            if (dt != null && dt.Rows.Count > 0)
            {

                if (string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
                {
                    allmsg =  DrpQK.Text + "：开账完成\r\n";
                  
                }
                else if (dt.Rows[0][0].ToString() == "没有错误，可以开帐；")
                {
                    allmsg =  DrpQK.Text + "：无数据需要开账\r\n";
                   
                }
                else
                {
                    allmsg = DrpQK.Text + "：" + dt.Rows[0][0].ToString() + "\r\n";
                }

            }
            return allmsg;
        }
        #endregion

    }
}